// AvenirSQL
const Socket = require('./tcp/socket');
const Core = require('./database/Core');
const User = require('./database/User');
const os = require('os');
const fs = require('fs');
const INI = require('ini');
const path = require('path');
const Logger = require('avenir-log');
const constants = require('./tools/constants');
const Conn = require('./database/Conn');
const Nodis = require('./database/Nodis');
const _ = require('lodash');
const nthFile = require('nth-file');
const Lock = require('./database/Lock');
const SQL = require('./database/Sql');
const Trans = require('./database/Trans');
const Trace = require('./database/Trace');
async function start() {
    let socket = new Socket();
    let sql = new SQL();
    let core = new Core();
    let user = new User();
    let file = await giveFileName();
    //如果没有ini文件就自创一个
    if (!fs.existsSync(file)) {
        defaultIni();
    }
    let ini = await readIni(file);
    let log = new Logger(ini.log.logName, ini.log.logPath);
    let conn = new Conn();
    //不希望没事就写日志 上生产了再说
    ini.db.debug || log.noDebug();
    global.log = log;
    global.socket = socket;
    //为了防止名称冲突
    global.define = {
        fileName: file,
    }
    global.ini = ini;
    global.db = core;       //core 名称冲突
    global.sql = sql;
    global.user = user;
    global.conn = conn;
    //nodis一定要创建在ini的后面 因为用到了ini的定义
    let nodis = new Nodis();
    global.nodis = nodis;
    let lock = new Lock();
    global._ = _;
    global.lock = lock;
    let trans = new Trans();
    global.trans = trans;
    let trace = new Trace();
    global.trace = trace;
    global.nthFile = nthFile;
    await socket.init(ini.main.ip, ini.main.port);//启动sock

    dbInit();
}

async function main() {
    await start();

}

async function giveFileName() {
    let type = os.platform();
    let fileName;
    if (type == "darwin" && constants.ifSingle) {
        fileName = "/Users/hideyoshi/Desktop/codes/avenir-db/run.ini";

    }
    else if (type == "win32" && constants.ifSingle) {
        fileName = "F:/avenir-db/run.ini";

    }
    else {
        fileName = "./run.ini"

    }
    return fileName;
}


async function readIni(fileName) {
    let file = fs.readFileSync(fileName);
    var info = INI.parse(file.toString());
    return info;
}


//初始化数据库配置信息
function dbInit() {
    let fileName = ini.name.rootSet;
    fileName = path.join(ini.main.path, fileName);
    if (!fs.existsSync(ini.main.path)) {
        fs.mkdirSync(ini.main.path, { recursive: true });
    }
    if (!fs.existsSync(fileName)) {
        fs.writeFileSync(fileName, '{}');        //默认写一个空的配置
    }
}

function defaultIni() {
    let ini = {
        main: {
            ip: '127.0.0.1',
            port: '44944',
            path: './db',
            ifConsoleLog: true
        },
        db: {
            keepAlive: false,
            debug: false,
            timeOut: '10000',
            maxColoums: '100',
            maxSqlLength: '200',
            user: 'User',
            cacheInvalid: '200',
            clearCache: '500',
            updateCache: '1',
            lockTimeOut: '3',
            lockTryTime: '10',
            releaseLockTime: '10',
            checkLockTime: '10'
        },
        name: {
            table: '.table',
            index: '.bpx',
            hash: '.hash',
            rootSet: 'AvenirSQL.json',
            dbSet: '.json'
        },
        curl: {}
    }
    fs.writeFileSync('./run.ini', INI.stringify(ini));
}

main();